﻿using System;
using System.Collections.Generic;
using System.Text;
using Boomerang.Common.Configuration;
using System.Threading;
using Boomerang.Common.Crm;
using Boomerang.Common.Sms;
using System.Diagnostics;

namespace Boomerang.Service.Threading
{
    public abstract class WorkerThreadBase
    {
        protected Organization org;
        private ManualResetEvent workDone;
        protected int threadIndex;
        private CrmConnection crmConnection;
        private SmsConnection smsConnection;
        protected CrmDataSource crmDataSource;
        protected SmsDataSource smsDataSource;

        public WorkerThreadBase(Organization org, ManualResetEvent resetEvent, int index)
        {
            Trace.TraceInformation("Initializing for Organization {0}",org);
            this.org = org;
            this.workDone = resetEvent;
            this.threadIndex = index;

            this.crmConnection = new CrmConnection(org.CrmConnectionOptions);
            this.smsConnection = new SmsConnection(org.SmsConnectionOptions);

            crmDataSource = new CrmDataSource(crmConnection);
            smsDataSource = new SmsDataSource(smsConnection);
            Trace.TraceInformation("Initizalized for Organization {0}", org);

        }

        public void ThreadPoolCallback(Object context)
        {
            Trace.TraceInformation("Begining work for Organization {0}", org);
            DoWork();
            Trace.TraceInformation("Finished work for Organization {0}", org);
            workDone.Set();
            Trace.TraceInformation("Notified end of work for Organization {0}", org);
        }

        protected abstract void DoWork();
    }
}
